/**
 * 重金属数据实现类
 */
package com.sc.datacollect.imple;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.sc.datacollect.util.Common;
import com.sc.datacollect.util.DateFormatUtils;
import com.sc.datacollect.util.MongoDb;
import com.sc.datacollect.util.SiteUtil;
import com.sc.datacollect.util.UnifiedCodePo;
import com.sc.datacollect.util.UnifiedcodeUtil;

public class HeavyMetal implements IProtocol {
	private JSONArray data;
	private MongoDb mongodb;

	public HeavyMetal(String dstype, String datasource, String datatype, JSONArray data, MongoDb mongodb) {
		this.data = data;
		this.mongodb = mongodb;
	}

	@Override
	public void initData() {
	}
	
	public String saveData() {
		try {
			DecimalFormat df = new DecimalFormat("0.00000");
			
			for(int i = 0; i < data.size(); i++) {
				// 格式化时间
				JSONObject o = data.getJSONObject(i);
				if (o.containsKey("PM2_5")) {
					o.put("pm2.5", o.get("PM2_5"));
					o.remove("PM2_5");
				}
				String dateStr = String.valueOf(o.get("time"));
				try {
					dateStr = DateFormatUtils.getDate_str(DateFormatUtils.getDate_util(dateStr.replace("/", "-")), 2).substring(0, 13) + ":00:00";
				} catch (Exception e) {}
				
				o.put("monitorTime", dateStr);
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				o.put("createTime", sdf.format(new Date()));
				// 保存原始数据
				
				List<UnifiedCodePo> pos = new LinkedList<UnifiedCodePo>();
				UnifiedCodePo po = null;
				for (String key : o.keySet()) {
					po = new UnifiedCodePo();
					po.setInCode(key);
					po.setCatalogCode("ITP_HM");
					pos.add(po);
				}
				
				// 调用编码转换
				pos = UnifiedcodeUtil.convert(pos);
				JSONObject convertObj = new JSONObject();
				for (String key : o.keySet()) {
					String myCode = UnifiedcodeUtil.getMyCode(pos, key);
					
					convertObj.put(myCode, o.get(key));
				}
				
				// 添加站点名称
				convertObj.put("siteName", SiteUtil.getSiteName(convertObj.getString("siteCode"), dateStr));
				
				mongodb.insert("ORI_ITP_HM_HOUR-" + dateStr.substring(0, 4), JSON.toJSONString(convertObj));
//				System.out.println(convertObj);
				
				// 查询前一小时pm2.5数据
				String hourago = sdf.format(new Date(sdf.parse(dateStr).getTime() - 3600000));
				DBObject qcolumns = new BasicDBObject();
				
				String pm25Code = UnifiedcodeUtil.getMyCode(pos, "pm2.5");
				qcolumns.put(pm25Code, 1);
				DBObject qquery = new BasicDBObject();
				qquery.put("monitorTime", hourago);
				DBObject obj = mongodb.selectOne("ORI_ITP_HM_HOUR-" + hourago.substring(0, 4), qcolumns, qquery);
				
				if (obj != null) {
					// 转换数据单位 /1000
					convertObj.put(pm25Code, obj.get(pm25Code));
				}
				List<String> strs = new ArrayList<String>();
				strs.add("monitorTime"); //监测时间
				strs.add("siteCode"); //站点编码
				strs.add("siteName"); //站点编码
				strs.add("deviceCode"); //设备编码
				strs.add("createTime"); //创建时间
				strs.add("time");
				strs.add(pm25Code);
				for (String key : convertObj.keySet()) {
					if (!strs.contains(key)) {
						convertObj.put(key, df.format(Double.parseDouble(convertObj.get(key) == null ? "0" : String.valueOf(convertObj.get(key))) * 0.001));
					}
				}
				// 插入转换单位后数据
				mongodb.insert("TRS_ITP_HM_HOUR-" + dateStr.substring(0, 4), JSON.toJSONString(convertObj));
//				System.out.println(convertObj);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return Common.EXCEPTIONCODE;
		}
		return Common.SUCCESSCODE;
	}

	
	public static void main(String[] args) {
		DecimalFormat df = new DecimalFormat("0.00000");
		System.out.println(Double.parseDouble(df.format(Double.parseDouble("2.53") * 0.001)));
	}
}
